//
//  OrderBaseControl.m
//  UEPersonage
//
//  Created by mac on 2017/10/18.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "OrderBaseControl.h"
#import "OrderWaitViewController.h"
#import "OrderIngViewController.h"
#import "UIView+Badge.h"
#import "MessageViewController.h"
#import "AboutJeemaaViewController.h"
#import "LoginViewController.h"
#import "LeftSortsViewController.h"

@interface OrderBaseControl ()<MMTabBarViewDataSource, MMTabBarViewDelegate,UIGestureRecognizerDelegate>{
    

}

@property (nonatomic, strong) NSMutableArray                                *dataArr;
@property (nonatomic, strong) UIView                                        *messageBageView;
@property (nonatomic, strong) UIButton                                      *messageButon;
@property (nonatomic, strong) UISegmentedControl                            *segment;
@property (nonatomic, strong) UIButton                                      *headView;
@property (nonatomic, strong) UIView                                        *topView;
@property (nonatomic, strong) UIView                                        *midView;
@property (nonatomic, strong) UIView                                        *footView;
@property (nonatomic, strong) UIImageView                                   *navBarHairlineImageView;

@end

@implementation OrderBaseControl


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    _navBarHairlineImageView.hidden = YES;
    //显示隐藏的导航栏部分子视图
    [_topView setHidden:NO];
    [_midView setHidden:NO];
    [_footView setHidden:NO];
    [_segment setHidden:NO];
    [_messageButon setHidden:NO];
    [_headView setHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    _navBarHairlineImageView.hidden = NO;
    //隐藏导航栏部分子视图
    [_topView setHidden:YES];
    [_midView setHidden:YES];
    [_footView setHidden:YES];
    [_segment setHidden:YES];
    [_messageButon setHidden:YES];
    [_headView setHidden:YES];

}

- (void)dealloc{
    
    [JeemaaNotificationCenter removeObserver:self name:@"openAboutJeemaa" object:nil];
    [JeemaaNotificationCenter removeObserver:self name:@"showRedBadge" object:nil];
    [JeemaaNotificationCenter removeObserver:self name:@"hiddenHudWhenLoginoutFaild" object:nil];
    [JeemaaNotificationCenter removeObserver:self name:@"recover" object:nil];
    [JeemaaNotificationCenter removeObserver:self name:@"loginoutAtLoading" object:nil];
    [JeemaaNotificationCenter removeObserver:self name:@"changOrderHead" object:nil];
    [JeemaaNotificationCenter removeObserver:self name:@"hiddenSha" object:nil];

}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.dataSource = self;
    self.delegate = self;
    [self setupOrderBar];
    [self layoutShadow];
    [self layoutNavigationBar];
    [self createSegmentControl];
    [self createleftNavigationTipView];
    [self registerNotification];
}

- (void)registerNotification{
    
    [JeemaaNotificationCenter addObserver:self selector:@selector(openAboutJeemaa) name:@"openAboutJeemaa" object:nil];
    [JeemaaNotificationCenter addObserver:self selector:@selector(showRedBadgeMethod) name:@"showRedBadge" object:nil];
    [JeemaaNotificationCenter addObserver:self selector:@selector(loginoutShowLoadingResponds) name:@"loginoutAtLoading" object:nil];
    [JeemaaNotificationCenter addObserver:self selector:@selector(hiddenHudWhenLoginoutFaildRes) name:@"hiddenHudWhenLoginoutFaild" object:nil];
    [JeemaaNotificationCenter addObserver:self selector:@selector(logoutRecoverResponds) name:@"recover" object:nil];
    [JeemaaNotificationCenter addObserver:self selector:@selector(changeheadView) name:@"changOrderHead" object:nil];
    [JeemaaNotificationCenter addObserver:self selector:@selector(showShadowView) name:@"hiddenSha" object:nil];
}

- (void)setupOrderBar{
    
    self.view.backgroundColor = RGB(245, 246, 247, 1);
    self.tabBarController.view.backgroundColor = [UIColor whiteColor];

    //初始化MMTabBar分段控件
    self.gradientType = MMTabBarViewGradientTypeUnderline;
    self.layoutType   = MMTabBarViewTextLayoutByEqualDivision;
    [self reload];
}

#pragma mark -- SetupUserfaceUI
- (void)layoutShadow{
    
    //侧滑阴影设置
    self.shadowView = [[UIView alloc]initWithFrame:self.view.bounds];
    self.shadowView.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.3];
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(openOrCloseLeftList)];
    recognizer.delegate = self;
    [self.shadowView addGestureRecognizer:recognizer];
    [self.shadowView setHidden:YES];
    [self.view addSubview:self.shadowView];
}

- (void)layoutNavigationBar{
    
    _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    _headView = [UIButton buttonWithType:UIButtonTypeCustom];
    _headView.backgroundColor = [UIColor whiteColor];
    _headView.frame = CGRectMake(16, 5, 28, 28);
    [_headView addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    _headView.layer.cornerRadius = 14;
    _headView.layer.masksToBounds = YES;
    [_headView setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:shareData.headURL] placeholderImage:[UIImage imageNamed:@"user"]];
    [self.navigationController.navigationBar addSubview:_headView];
    
    _messageBageView = [[UIView alloc]initWithFrame:CGRectMake(Screen_Width - 15-22, 8, 20, 20)];
    _messageBageView.backgroundColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:_messageBageView];
    if ([[UserDefaults objectForKey:@"unreadMessage"] boolValue] == YES) {
        [self showRedBadgeMethod];
    }
    else{
        [_messageBageView removeBadgeView];
    }
    
    _messageButon = [UIButton buttonWithType:UIButtonTypeCustom];
    _messageButon.backgroundColor = [UIColor clearColor];
    _messageButon.frame = CGRectMake(Screen_Width - 15 - 20, 5, 20, 20);
    [_messageButon addTarget:self action:@selector(rightViewResponds) forControlEvents:UIControlEventTouchUpInside];
    _messageButon.layer.cornerRadius = 10;
    _messageButon.layer.masksToBounds = YES;
    [_messageButon setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:@"nil"] placeholderImage:[UIImage imageNamed:@"消息"]];
    [self.navigationController.navigationBar addSubview:_messageButon];
    
}

- (void)createSegmentControl{
    
    //添加分段控制器
    NSArray *array = [NSArray arrayWithObjects:@"待抢单",@"进行中", nil];
    _segment = [[UISegmentedControl alloc]initWithItems:array];
    _segment.frame = CGRectMake(10, 100, 152*Radio, 28);
    _segment.center = CGPointMake(self.view.center.x, 20);
    NSDictionary *selected = @{NSFontAttributeName:[UIFont systemFontOfSize:11],
                               NSForegroundColorAttributeName:[UIColor whiteColor]};
    NSDictionary *normal = @{NSFontAttributeName:[UIFont systemFontOfSize:11],
                             NSForegroundColorAttributeName:RGB(133, 136, 146, 1)};
    [_segment setTitleTextAttributes:normal forState:UIControlStateNormal];
    [_segment setTitleTextAttributes:selected forState:UIControlStateSelected];
    _segment.tintColor = RGB(133, 136, 146, 1);
    _segment.selectedSegmentIndex = orderSingleton.segementIndex;
    [_segment addTarget:self action:@selector(SelectTeamCliced:) forControlEvents:UIControlEventValueChanged];
    [self.navigationController.navigationBar addSubview:_segment];
}
//三个点
- (void)createleftNavigationTipView{
    
    _topView = [[UIView alloc]initWithFrame:CGRectMake(7*Radio, iPhoneX ? 10*Radio-3 : 10*Radio-1, 2.6, 2.6)];
    _topView.backgroundColor = RGB(134, 137, 147, 1);
    _topView.layer.cornerRadius = 1.3;
    _topView.layer.masksToBounds = YES;
    [self.navigationController.navigationBar addSubview:_topView];
    
    _midView = [[UIView alloc]initWithFrame:CGRectMake(7*Radio, iPhoneX ? 20*Radio-4 : 20*Radio-2, 2.6, 2.6)];
    _midView.backgroundColor = RGB(134, 137, 147, 1);
    _midView.layer.cornerRadius = 1.3;
    _midView.layer.masksToBounds = YES;
    [self.navigationController.navigationBar addSubview:_midView];
    
    _footView = [[UIView alloc]initWithFrame:CGRectMake(7*Radio, iPhoneX ? 30*Radio-5 : 30*Radio-3, 2.6, 2.6)];
    _footView.backgroundColor = RGB(134, 137, 147, 1);
    _footView.layer.cornerRadius = 1.3;
    _footView.layer.masksToBounds = YES;
    [self.navigationController.navigationBar addSubview:_footView];
}


#pragma mark -- MMTabBarViewDataSource
- (NSArray *)infomationsForViewController:(MMTabBarViewController *)tabBarViewController{
    return [self.dataArr copy];
}


- (NSUInteger )numberOfItemsInViewController:(MMTabBarViewController *)tabBarViewController {
    return self.dataArr.count;
}


- (MMTabBarModel *)infomationInViewController:(MMTabBarViewController *)tabBarViewController infoForItemAtIndex:(NSUInteger)index {
    return self.dataArr[index];
}

#pragma mark -- MMTabBarViewDelegate
- (UIColor *)tabBarViewControllerShowTitleScrollViewBackgroudColor:(MMTabBarViewController *)tabBarViewController{
    return [UIColor whiteColor];
}

#pragma mark -- changeheadView
- (void)changeheadView{
    
    [_headView setImageForState:UIControlStateNormal withURL:[NSURL URLWithString:shareData.headURL] placeholderImage:[UIImage imageNamed:@"user"]];
}

#pragma mark -- messageNotRead
- (void)showRedBadgeMethod{
    
    [_messageBageView yee_MakeBadgeText:@"" textColor:[UIColor whiteColor] backColor:[UIColor redColor] Font:[UIFont systemFontOfSize:10]];
}

#pragma mark -- selectTeamDelegate
- (void)SelectTeamCliced:(UISegmentedControl *)sender{
    
    //切换团队和平台时发送通知
    dispatch_async(dispatch_get_main_queue(), ^{
        //等待主线程执行完毕
        orderSingleton.segementIndex = sender.selectedSegmentIndex;
        [JeemaaNotificationCenter postNotificationName:@"exchangeSegementAndReloadData_wait" object:nil];
        [JeemaaNotificationCenter postNotificationName:@"exchangeSegementAndReloadData_ing" object:nil];
    });
}

#pragma mark -- LeftListControl
- (void)openOrCloseLeftList{
    
//    [self.sideMenuViewController presentLeftMenuViewController];
}

#pragma mark -- shadowViewControl
- (void)hiddenShadowView{
    [self.shadowView setHidden:NO];
}

- (void)showShadowView{
    [self.shadowView setHidden:YES];
}

- (void)hiddenHudWhenLoginoutFaildRes{
    
    [LSProgressHUD hideForWindow];

}

#pragma mark -- pushToMessageVC
- (void)rightViewResponds{
    
    //打开消息中心
    [_messageBageView removeBadgeView];
    [UserDefaults setBool:NO forKey:@"unreadMessage"];
    self.hidesBottomBarWhenPushed = YES;
    MessageViewController *msgVC = [[MessageViewController alloc]init];
    [self.navigationController pushViewController:msgVC animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    
}

#pragma mark -- openAboutJeemaa
- (void)openAboutJeemaa{
    
    self.tabBarController.selectedIndex = 0;
    AboutJeemaaViewController *jeemaaa = [[AboutJeemaaViewController alloc] init];
    [self.navigationController pushViewController:jeemaaa animated:YES];
}

#pragma mark -- loginoutResponds

- (void)loginoutShowLoadingResponds{
    
    [LSProgressHUD showOnWindow];
}

- (void)logoutRecoverResponds{
    
    [LSProgressHUD hideForWindow];
    _segment.selectedSegmentIndex = 0;
    orderSingleton.segementIndex = 0;

    //切换到登录界面
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.window.rootViewController dismissViewControllerAnimated:NO completion:nil];
    tempAppDelegate.window.rootViewController = nil;
    tempAppDelegate.window.rootViewController = [LoginViewController new];
}

//找出导航栏下面的黑线(findHairlineImageViewUnder):
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

#pragma mark -- getter
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        NSArray *data = @[@[@"OrderWaitViewController",@"待抢单"],
                          @[@"OrderIngViewController",@"进行中"]];
        for (int i = 0; i < data.count; i++) {
            [_dataArr addObject:[MMTabBarModel modelWithControllerClassName:[data[i] firstObject]  controllerTitle:[data[i] lastObject]]];
        }
    }
    return _dataArr;
}


@end

